package com.cooper.scraper;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources.NotFoundException;
import android.util.Log;

public class ScraperException extends Exception {

	/**
	 * 
	 */
	public static final int ERROR_XML = 111;
	public static final int ERROR_IO = 112;
	public static final int ERROR_NOT_FOUND = 113;
	public static final int ERROR_OTHER = 114;

	private static final long serialVersionUID = -1866050074424894216L;
	private Exception e;
	private String functionName;

	public ScraperException() {
		super();
		functionName = "";
	}

	public ScraperException(String functionName, Exception e) {
		super(e.getMessage());
		this.functionName = functionName;
		this.e = e;
		Log.e("ScraperException", e.getMessage() + ":" + functionName, e);
	}

	public ScraperException(Exception e) {
		super(e.getMessage());
		this.e = e;
		Log.e("ScraperException", e.getMessage(), e);
	}

	public String getFunctionName() {
		return functionName;
	}

	public int getErrorCode() {
		if (e == null) {
			return ERROR_NOT_FOUND;
		}

		if (e instanceof XmlPullParserException) {
			return ERROR_XML;
		} else if (e instanceof NotFoundException) {
			return ERROR_NOT_FOUND;
		} else if (e instanceof IOException) {
			return ERROR_OTHER;
		} else
			return ERROR_OTHER;
	}

}
